package com.demo2.todo.model.entity;

import jakarta.persistence.*;

import lombok.Data;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * @author evi
 */
@Data
@Entity
@Table(name = "users")
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true, nullable = false)
    private String username;

    @Column(nullable = false)
    private String password;

    @Column(nullable = false)
    private boolean enabled = true;

    @CreationTimestamp
    private LocalDateTime createdAt;

    @Column(nullable = false)
    private String role;

    // 实现权限获取方法
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 角色名称需要以"ROLE_"前缀开头（Spring Security规范）
        return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + role));
    }

}
